//
//  AuthorViewController.m
//  Comic
//
//  Created by Andy.He on 16/3/12.
//  Copyright © 2016年 程锦. All rights reserved.
//

#import "AuthorViewController.h"
#import "AuthorTableViewCell.h"
#import "TopicModel.h"
#import "AuthorModel.h"
#define USER_URL @"http://api.kuaikanmanhua.com/v1/users/"

@interface AuthorViewController () <UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *authorTableView;
@property (strong, nonatomic) IBOutlet UIView *headerView;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *infoLabel;
@property (strong, nonatomic) IBOutlet UIImageView *headerImage;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *imageLeft;

@property (strong, nonatomic) NSMutableArray *topicDataArr;
@property (strong, nonatomic) NSMutableArray *authorDataArr;


@end

@implementation AuthorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self headerViewCreate];
    [self dateJson];
//    [self createTable];
}

- (void)headerViewCreate{
    self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3);
    self.authorTableView.tableHeaderView = self.headerView;
    self.imageLeft.constant = (SCREEN_WIDTH - 100) / 2;
    self.headerImage.layer.cornerRadius = 50;
    self.headerImage.layer.masksToBounds = YES;
    [self.authorTableView registerNib:[UINib nibWithNibName:NSStringFromClass([AuthorTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([AuthorTableViewCell class])];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    rightButton.frame = CGRectMake(0, 0, 18, 18);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    [rightButton addTarget:self action:@selector(clickleft:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)dateJson{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@%@",USER_URL,self.authorUrl];
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.topicDataArr = [NSMutableArray array];
        self.authorDataArr = [NSMutableArray array];
        [self.headerImage sd_setImageWithURL:[NSURL URLWithString:responseObject[@"data"][@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"header_image"]];
        self.headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backPic"]];
        self.nameLabel.text = responseObject[@"data"][@"nickname"];
        self.infoLabel.text = responseObject[@"data"][@"intro"];
        self.infoLabel.numberOfLines = 0;
        [self.infoLabel sizeToFit];
        
        for (NSDictionary *dic in responseObject[@"data"][@"topics"]) {
            TopicModel *topicMod = [[TopicModel alloc] init];
            [topicMod setValuesForKeysWithDictionary:dic];
            [self.topicDataArr addObject:topicMod];
        }
        
        [self.authorTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.topicDataArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AuthorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AuthorTableViewCell class]) forIndexPath:indexPath];
    TopicModel *topicMod = self.topicDataArr[indexPath.row];
    cell.titleLab.text = topicMod.title;
    cell.detailLab.text = topicMod.topic_description;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:[topicMod.cover_image_url stringByReplacingOccurrencesOfString:@"750" withString:@"640"]] placeholderImage:[UIImage imageNamed:@"placeholder_pic"]];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    UILabel *textLab = [[UILabel alloc] initWithFrame:sectionView.frame];
    textLab.text = @"TA的作品";
    [sectionView addSubview:textLab];
    return sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (void)clickleft:(UIButton *)button{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
